package com.gsm.projects.jobDw.mock;

import com.github.javafaker.Faker;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Random;

public class EmploymentDataGenerator {

    public static void main(String[] args) {
        // 数据库配置
        String url = "jdbc:mysql://hadoop102:3306/db_job_realtime_statistics?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
        String user = "root";
        String password = "root";

        // 毕业生数量
        int numberOfGraduates = 10;

        // 雇主数量
        int numberOfEmployers = 10;

        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            Faker faker = new Faker(new Locale("zh_CN"));
            Random random = new Random();

            String sql = "INSERT INTO employmentrecords (graduate_id, employer_id, send_offer_time, onboarding_date, position, salary, employment_type) VALUES (?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement stmt = conn.prepareStatement(sql);

            for (int i = 1; i <= 10; i++) {

                int graduateId = random.nextInt(numberOfGraduates)+1; // // 生成1到2869001之间的毕业生
                int employerId = random.nextInt(numberOfEmployers) + 1; // 生成1到100之间的随机雇主ID
                java.util.Date sendOfferTime = faker.date().between(java.util.Date.from(java.time.LocalDate.of(2020, 1, 1).atStartOfDay().toInstant(java.time.ZoneOffset.UTC)), java.util.Date.from(java.time.LocalDate.now().atStartOfDay().toInstant(java.time.ZoneOffset.UTC)));
                java.util.Date onboardingDate = faker.date().between(sendOfferTime, java.util.Date.from(java.time.LocalDate.now().plusDays(30).atStartOfDay().toInstant(java.time.ZoneOffset.UTC))); // 报到日期在发offer后的30天内
                String position = faker.job().position();
                double salary = faker.number().randomDouble(2, 5000, 30000); // 薪资范围5000到30000
                String employmentType = faker.options().option("全职", "兼职", "实习");

                stmt.setInt(1, graduateId); // graduate_id
                stmt.setInt(2, employerId); // employer_id
                stmt.setTimestamp(3, new java.sql.Timestamp(sendOfferTime.getTime())); // send_offer_time
                stmt.setDate(4, new java.sql.Date(onboardingDate.getTime())); // onboarding_date
                stmt.setString(5, position); // position
                stmt.setBigDecimal(6, new java.math.BigDecimal(salary)); // salary
                stmt.setString(7, employmentType); // employment_type

                stmt.addBatch();

//                if (graduateId % 1000 == 0 || graduateId == numberOfGraduates) { // 批量执行以提高效率
//                    stmt.executeBatch();
//                }
            }

            stmt.executeBatch();

            System.out.println("Data generation and insertion completed successfully.");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}